package Schedule::Chronic::Distributed::TaskRequirement;

=head1 NAME

Schedule::Chronic::Distributed::TaskRequirement - Task Requirements

=head1 SYNOPSIS

use Schedule::Chronic::Distributed::TaskRequirement;

.. TODO.. 

=head1 DESCRIPTION

Task Requirement class, maps tasks to requirements and required levels.

=head1 METHODS

=head2 ACCESSORS

=over

=item id 

=item requirement 

=item task 

=item parameters

=item parameter

=back

=head2 inflate_parameters / deflate_parameters

inflate and deflate parameters for retrievel/storage via CDBI

=head2 METHODS PROVIDED ELSEWHERE

Additional methods for the Monitor Web UI are provided in
Schedule::Chronic::Distributed::Monitor::TaskRequirement package
which inherits from Maypole::Model::CDBI.

=cut

use strict;

use base qw(Schedule::Chronic::Distributed::DBI);

Schedule::Chronic::Distributed::TaskRequirement->table('task_requirement');
Schedule::Chronic::Distributed::TaskRequirement->columns(All => qw/id requirement task parameters/);
Schedule::Chronic::Distributed::TaskRequirement->columns(Essential => qw/id requirement task parameters/);
Schedule::Chronic::Distributed::TaskRequirement->columns(Primary => 'id');
Schedule::Chronic::Distributed::TaskRequirement->columns(TEMP => 'inflated_params');

Schedule::Chronic::Distributed::TaskRequirement->has_a(task => 'Schedule::Chronic::Distributed::Task');
Schedule::Chronic::Distributed::TaskRequirement->has_a(requirement => 'Schedule::Chronic::Distributed::Requirement');

__PACKAGE__->add_trigger(before_update  => \&deflate_parameters);
__PACKAGE__->add_trigger(select         => \&inflate_parameters);

sub parameters {
    my ($self,$values) = shift;
    if ($values) {
      foreach (keys %$values) {
	$self->{inflated_params}{$_} = $values->{$_};
      }
    } else {
      return $self->{inflated_params};
    }
}

sub parameter {
    my ($self,$key,$value) = @_;
    $self->{inflated_params}{$key} = $value;
    return;
}

sub inflate_parameters {
    my $self = shift;
    if ($self->{inflated_params}) {
      warn "inflate_parameters has already been called, called again by : ", join(', ',caller);
      return $self->{inflated_params};
    }
    my $params = { map { split(/[\=\:]/,$_) } split(/\s*\,\s*/,$self->get('parameters')) };
    $self->{inflated_params} = $params;
    return $params;
}

sub deflate_parameters {
  my $self = shift;
  my $params = $self->{inflated_params};
  my $deflated_params = '';
  if ($params) {
    $deflated_params = join (',',map ( "$_=".$params->{$_}, keys %$params ));
    $self->_attribute_set('parameters',$deflated_params);
  } else {
    $deflated_params = $self->_attribute_get('parameters');
  }
  return $deflated_params;
}

1;

__END__


=head1 SEE ALSO

Schedule::Chronic::Distributed::Task

Schedule::Chronic::Distributed::Requirement

=head1 AUTHOR

aaron trevena, E<lt>aaron.trevena@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Aaron Trevena, Aviation Briefing Ltd

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.

=cut
